package com.handpay.zztong.hp;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.utils.Heart;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.paymentbill.model.Bill;

import java.util.Hashtable;

public class AppealActivity extends ZZTongActivity {

    private RadioButton appeal_one_rb, appeal_two_rb, appeal_three_rb;
    private RadioGroup appeal_rg;
    private EditText appeal_other_et;
    private Button appeal_btn;
    private AlertDialog dialog;
    /*
     * 0：交易款项未到账、 1：交易手续费计算错误、 2：其它 当该字段为2时appealContent必须有值
     */
    private static int APPEAL_STATUS_ONE = 0, APPEAL_STATUS_TWO = 1,
            APPEAL_STATUS_THREE = 2;
    private int mCurrentAppealType = 0;
    private Bill mBill;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_appeal);
        super.onCreate(savedInstanceState);
        mBill = (Bill) this.getIntent().getSerializableExtra(ACTION);
        appeal_rg = (RadioGroup) this.findViewById(R.id.appeal_rg);
        appeal_one_rb = (RadioButton) this.findViewById(R.id.appeal_one_rb);
        appeal_two_rb = (RadioButton) this.findViewById(R.id.appeal_two_rb);
        appeal_three_rb = (RadioButton) this.findViewById(R.id.appeal_three_rb);
        appeal_other_et = (EditText) this.findViewById(R.id.appeal_other_et);
        appeal_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.appeal_three_rb) {
                    mCurrentAppealType = APPEAL_STATUS_THREE;
                    appeal_other_et.setEnabled(true);
                } else {
                    appeal_other_et.setEnabled(false);
                    appeal_other_et.setText("");
                    if (checkedId == R.id.appeal_one_rb) {
                        mCurrentAppealType = APPEAL_STATUS_ONE;
                    } else if (checkedId == R.id.appeal_two_rb) {
                        mCurrentAppealType = APPEAL_STATUS_TWO;
                    }
                }
            }
        });
        appeal_btn = (Button) this.findViewById(R.id.appeal_btn);
        appeal_btn.setOnClickListener(this);
    }

    /***
     * 账账通账户退出
     */
    private void doTransactionAppeal() {
        if (mCurrentAppealType == APPEAL_STATUS_THREE && TextUtils.isEmpty(appeal_other_et.getText().toString().trim())) {
            Toast.makeText(AppealActivity.this, "请填写申诉内容", Toast.LENGTH_SHORT).show();
            return;
        }
        Hashtable<String, String> params = new Hashtable<String, String>();
        params.put("channel", ClientEngine.CHANNEL);
        if (mCurrentAppealType == APPEAL_STATUS_THREE) {
            params.put("appealType", "2");
            params.put("appealContent", appeal_other_et.getText().toString());
        } else if (mCurrentAppealType == APPEAL_STATUS_ONE) {
            params.put("appealType", "0");
        } else if (mCurrentAppealType == APPEAL_STATUS_TWO) {
            params.put("appealType", "1");
        }
        params.put("status", "1");
        params.put("transTime", mBill.getTransTime());
        params.put("transSN", mBill.getTransSN());
        doPost(this, Do_TransactionAppeal, params);

    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(getString(R.string.appeal_title), true);
    }

    @Override
    public boolean netResponse(String action, Hashtable<String, Object> resp,
                               boolean showAlert) {
        if (super.netResponse(action, resp, showAlert)) {
            return true;
        }
        if (action.equals(Do_TransactionAppeal)) {

            showDialogTips("提示", "您的申诉已提交成功，我们将尽快处理后并给您回复，您可在申诉记录中查询处理进度。",
                    "确定");


        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.appeal_btn) {
            doTransactionAppeal();
        }
    }

    private void showDialogTips(final String title, final String text,
                                final String OkBtnText) {
        class RunnableShowAlertDialog implements Runnable {
            @Override
            public void run() {
                if (dialog != null && dialog.isShowing()) {
                    return;
                }
                LayoutInflater inflater = LayoutInflater
                        .from(AppealActivity.this);
                View windov = inflater.inflate(R.layout.dialog_tips, null);
                TextView msg = (TextView) windov.findViewById(R.id.msg);
                msg.setText(text);
                TextView title_tv = (TextView) windov.findViewById(R.id.title);
                title_tv.setText(title);

                Button sure = (Button) windov.findViewById(R.id.bt_sure);
                sure.setText(OkBtnText);
                sure.setOnClickListener(new android.view.View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        setResult(RESULT_OK);
                        finish();
                    }
                });

                dialog = new AlertDialog.Builder(AppealActivity.this).show();
                Window dialogWindow = dialog.getWindow();
                Heart.initialize(AppealActivity.this);
                WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
                p.width = (int) (Heart.displayWidthPixels * 0.8); // 宽度设置为屏幕的0.65
                // p.height = p.width * 3 / 4;
                dialogWindow.setAttributes(p);
                dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

                try {
                    dialog.setCancelable(false);// 不可以用“返回键”取消//
                } catch (Exception e) {
                }
                dialog.setContentView(windov);
            }
        }
        this.runOnUiThread(new RunnableShowAlertDialog());
    }
}
